<template>
  <div class="myfavor">
    <van-nav-bar :title="title" left-text="返回" left-arrow @click-left="() => $router.go(-1)" />
    <!-- 我的发布 -->
    <div class="release" v-if="title === '我的发布'">
      <div v-if="list[0].length === 0">
        <p class="empty">你的列表里竟然空空如也~~</p>
      </div>
      <div v-else>
        <momentStrip
          v-for="item in list[0]"
          :key="item.momentID"
          :pieceData="item"
          :showOpera="true"
        >
          <template #delete>
            <div @click="deleteMoment(item.momentID)">删除</div>
          </template>
        </momentStrip>
      </div>
    </div>
    <!-- 我的关注 -->
    <div class="care" v-else-if="title === '我的关注'">
      <div v-if="list[1].length === 0">
        <p class="empty">你的列表里竟然空空如也~~</p>
      </div>
      <div v-else>
        <friendStrip
          v-for="item in list[1]"
          :key="item.userID"
          :pieceData="item"
        />
      </div>
    </div>
    <div class="like" v-else-if="title==='我的喜欢'">
      <div v-if="list[2].length === 0">
        <p class="empty">你的列表里竟然空空如也~~</p>
      </div>
      <div v-else>
        <div>
          <momentStrip
            v-for="item in list[2]"
            :key="item.momentID"
            :pieceData="item"
            :showOpera="true"
          >
          </momentStrip>
        </div>
      </div>
    </div>
    <div class="like" v-else>
      <div v-if="list[3].length === 0">
        <p class="empty">你的列表里竟然空空如也~~</p>
      </div>
      <div v-else>
        <div>
          <orderStrip
            v-for="item in list[3]"
            :key="item.momentID"
            :pieceData="item"
            :userID="userID"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import momentStrip from "@/components/momentStrip";
import friendStrip from "@/components/friendStrip";
import orderStrip from "@/components/orderStrip";
import { Dialog } from "vant";
export default {
  components: {
    momentStrip,
    friendStrip,
    orderStrip,
  },
  data() {
    return {
      userID:'',
      title: "",
      list: [[], [], [],[]],
    };
  },
  methods: {
    getList() {
      let origin = this.$route.query.origin;
      let userID = this.$route.query.userID;
      this.userID=userID
      if (origin === "like") {
        this.title = "我的喜欢";
        this.$axios.get(`/getMomentsLike?userID=${userID}`).then((res) => {
          this.$set(this.list, 2, res.data);
        });
      } else if (origin === "released") {
        this.title = "我的发布";
        this.$axios.get(`/getMomentsReleased?userID=${userID}`).then((res) => {
          this.$set(this.list, 0, res.data);
        });
      } else if (origin === "star") {
        this.title = "我的关注";
        this.$axios.get(`/getUserCare?userID=${userID}`).then((res) => {
          this.$set(this.list, 1, res.data);
        });
      } else if (origin === "goods") {
        this.title = "我的商品";
        this.$axios.get(`/getMyGoodsOrder?userID=${userID}`).then((res) => {
          this.$set(this.list, 3, res.data);
        });
      }
    },
    deleteMoment(momentID) {
      let that = this;
      function beforeClose(action, done) {
        if (action === "confirm") {
          this.$axios.post(`/deleteMoment?momentID=${momentID}`).then((res) => {
            if (res.code === 200) {
              that.getList();
              done();
            }
          });
        } else {
          done();
        }
      }
      Dialog.confirm({
        title: "删除确认",
        message: "是否确认删除？",
        beforeClose,
      });
    },
  },
  created() {
    this.getList();
    this.userID=parseInt(localStorage.getItem("userID"))
  },
};
</script>

<style scoped>
.head {
  font-weight: bold;
}
.empty {
  color: gray;
  font-size: 10px;
}
</style>